package com.abner.springcloudprovideruser.Service;

import com.abner.springcloudprovideruser.entity.SecurityUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @Author: Abner.Jiang
 * @Description:
 * @Date: Created on 2019/12/20 10:10
 * @ProjectName: springcloud-provider-user
 */
@Component
public class CustomUserDetailService implements UserDetailsService {

    /**
     * 模拟两个账户：
     * 1：账户是 user，密码是user，角色是 user-role
     * 2：账户是 admin，密码是 admin，角色是 admin-role
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if ("user".equals(username)){
            return new SecurityUser("user","user","user-role");
        } else if ("admin".equals(username)){
            return new SecurityUser("admin","admin","admin-role");
        } else {
            throw new UsernameNotFoundException(username);
        }
    }
}
